<?php 
	
if (!isset($_SESSION)) {
  session_start();
  if(!isset($_SESSION['MM_Username']))
  	header("location:../../index.php");
}
	require_once('../../Connections/microassur.php'); 
	require_once('../../utils/fonctions.php');	
	include_once '../../include/inc_session.php';
	$message = '';

	if(isset($_SESSION['iduser'])){
		//$idAgent = idAgent($database_microassur, $microassur, $_SESSION['iduser']); //var_dump($idAgent);
		//var_dump($_SESSION['iduser']);
	}
?>

<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

$currentPage = $_SERVER["PHP_SELF"];

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if(isset($_GET['modif']))
	$modif = $_GET['modif'];
else
	$modif = 0;	


$maxRows_rs1 = 10;
$pageNum_rs1 = 0;
if (isset($_GET['pageNum_rs1'])) {
  $pageNum_rs1 = $_GET['pageNum_rs1'];
}
$startRow_rs1 = $pageNum_rs1 * $maxRows_rs1;

$colname_rs1 = "-1";

if ( ((isset($_POST["MM_rechercher"])) && ($_POST["MM_rechercher"] == "form1")) || ((isset($_GET["MM_rechercher"])) && ($_GET["MM_rechercher"] == "form1")) ) 
{
	if (isset($_POST['Numero_contrat']) && !empty($_POST['Numero_contrat'])) 
	{
	  $numero_contrat = $_POST['Numero_contrat'];
	  $colname_rs1 = IdContrat($database_microassur, $microassur, $numero_contrat);
	  $_GET['Numero_contrat'] = $numero_contrat;
	  //header('location:index.php?MM_rechercher=form1&Numero_contrat='.$numero_contrat);
	}
	
	if(isset($_GET['Numero_contrat']) && !empty($_GET['Numero_contrat']))
	{
	  $numero_contrat = $_GET['Numero_contrat'];
	  $colname_rs1 = IdContrat($database_microassur, $microassur, $numero_contrat);
	}

	mysql_select_db($database_microassur, $microassur);
	//$query_rs1 = sprintf("SELECT * FROM `role`  WHERE Etat_role = 'I' AND Identifiant_contrat = %s", GetSQLValueString($colname_rs1, "int"));
	//FS on fournit juste la liste des assurés
	$query_rs1 = sprintf("SELECT * FROM `role`  WHERE Identifiant_contrat = %s AND Etat_role = 'A' AND Type_role = 'A' ", GetSQLValueString($colname_rs1, "int"));
	//$query_limit_rs1 = sprintf("%s LIMIT %d, %d", $query_rs1, $startRow_rs1, $maxRows_rs1);
	$rs1 = mysql_query($query_rs1, $microassur) or die(mysql_error());
	$row_rs1 = mysql_fetch_assoc($rs1);
	
	if (isset($_GET['totalRows_rs1'])) 
	{
	  $totalRows_rs1 = $_GET['totalRows_rs1'];
	} else 
	{
		$all_rs1 = mysql_query($query_rs1);
	  	$totalRows_rs1 = mysql_num_rows($all_rs1);
	  	//$totalPages_rs1 = ceil($totalRows_rs1/$maxRows_rs1)-1;

		//**************************
		mysql_select_db($database_microassur, $microassur);
		$query_rs2 = sprintf("SELECT * FROM `contrat`  WHERE Identifiant_contrat = %s", GetSQLValueString($colname_rs1, "int"));
		$rs2 = mysql_query($query_rs2, $microassur) or die(mysql_error());
		$row_rs2 = mysql_fetch_assoc($rs2);
	
		$all_rs2 = mysql_query($query_rs2);
		$totalRows_rs2 = mysql_num_rows($all_rs2);
		//**************************
		
		$etatContrat = EtatContrat($database_microassur, $microassur, @$numero_contrat);
	
		if((isset($_POST["MM_rechercher"])) && ($_POST["MM_rechercher"] == "form1"))
		{
			if(isset($_POST['Numero_contrat']) && empty($_POST['Numero_contrat']))
			{
				$message = colorer("Vous devez saisir le numéro de contrat", "false");
			}
			elseif(isset($_POST['Numero_contrat']) && !veriflibelle1('Numero_contrat'))
			{
				$message = colorer("Le numéro de contrat saisi comporte des caractères spéciaux interdits.","false");
			}
			elseif( $totalRows_rs2==0)
			{
				$message = colorer("Contrat inexistant", "false");
			}
			elseif($etatContrat == 'R')
			{
				$message = colorer("Vous ne pouvez pas radier un assuré sur un contrat résilié", "false");
			}
			/*elseif($totalRows_rs1==0)
			{
				$message = colorer("", "false"); //colorer("Aucun role n'est rattaché à ce contrat.", "false"); // Contrat inexistant ou n'est rattaché à aucun rôle
			}*/
			else
			{
				//FS: S’il y a un seul assuré, on doit résilier le contrat
				$oContrat = new dataObject_contrat();
				$idContrat = $oContrat->contrat_getIdFromNumContrat($numero_contrat);
				$aAssure = $oContrat->contrat_getListAssureActif($idContrat);
				//$aAssure = $oContrat->contrat_getCountIntervenantActif($IdContrat);
	          if(isset($aAssure) && count($aAssure)==1) 
				//	$message = colorer(htmlentities(contrat_a_resilier, ENT_COMPAT, 'utf-8'), "false");
					$message = colorer("Vous devez résilier le contrat", "false");
					
			 	//var_dump(count($aAssure));
			}
		}
		
		if((isset($_GET["MM_rechercher"])) && ($_GET["MM_rechercher"] == "form1"))
		{
				//FS: S’il y a un seul assuré, on doit résilier le contrat
				$oContrat = new dataObject_contrat();
				$idContrat = $oContrat->contrat_getIdFromNumContrat($numero_contrat);
				$aAssure = $oContrat->contrat_getListAssureActif($idContrat);
				if(isset($aAssure) && count($aAssure)==1) 
					$message = colorer("Vous devez résilier le contrat", "false");
		}
	}
} // Fin if ((isset($_POST["MM_rechercher"])) && ($_POST["MM_rechercher"] == "form1"))

if(isset($_GET['annul']) && $_GET['annul']==0){
	$message = colorer("Vous devez cliquer sur le bouton Valider car vous avez modifié un des intervenants.", "false");
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
		<meta http-equiv="X-UA-Compatible" content="IE=8" />
			<title>Radiation assure</title>
			<link rel="stylesheet" type="text/css" href="../../css/main.css" />
            <link rel="stylesheet" type="text/css" href="../../css/radiation.css" />
			<!--[if IE 7]>
            	<link rel="stylesheet" type="text/css" href="../../css/ie7.css" />
            	<link rel="stylesheet" type="text/css" href="form_ie7.css" />
            <![endif]-->
			<script language="Javascript">
				<!--
					function modifier(choix){
                    var modifAct = document.getElementById("modifAct");
					var visuaAct = document.getElementById("VisuaAct");
                    modifAct.href = "intervenants.php?Identifiant_personne=" + choix + '&Numero_contrat=' + '<?php echo $numero_contrat; ?>';
					visuaAct.href = "intervenants.php?Identifiant_personne=" +  choix + '&Numero_contrat=' + '<?php echo $numero_contrat; ?>';
                }
					
					
				
					
					
				//-->
            </script>
	</head> 

<body>
<div id="sf_menu1">
	<h3>Production</h3>
<?php
	if(isset($message) && !empty($message)){
		echo $message;
	}
	$nommodule="Radiation Assuré";
	$nom_module_saisie="Liste des Intervenants";
?>
</div>
<div class="cleared"></div>
<div class="corps1">

	<form action="<?php echo $editFormAction; ?>" method="post" name="form1" id="form1">
		<div class="form1">
			<span>N&#176; CONTRAT :</span>
			<input id="Numero_contrat"  class="resize_1" type="text" name="Numero_contrat" value="<?php if(isset($_GET['Numero_contrat'])){ echo $_GET['Numero_contrat']; } else{ echo remind_text('Numero_contrat');} ?>" style="width:170px"  maxlength="20" />
		        <div class="bt "><span class="btleft"></span><input type="submit" value="Rechercher" class="resizeall btmiddle" /><span class="btright"></span></div>
		</div>
			
		<input type="hidden" name="MM_rechercher" value="form1" />
	</form>
	<div class="cleared"></div>
	<div id="br"></div>

	<div class="imgleft2"></div>
    <div class="imgmiddle2">
    <div class="btn">
        	<span class="btnleft"></span>
            <span class="btnmiddle">
            	<h5><?php echo $nommodule; ?></h5>
            </span><span class="btnright"></span>
        </div>
        <div class="btn1">
        	<span class="btnleft"></span>
            <span class="btnmiddle">
            	<h5><?php echo $nom_module_saisie; ?></h5>
            </span><span class="btnright"></span>
        </div>
    <div class="cleared" ></div>
	
    <table class="tab_liste">
       <tr id="niv1">
		  	<td class="col1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>
		    <td class="col2">ROLE&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>
		    <td class="col3">TYPE ASSURE</td>
		    <td class="col4">NOM</td>
		    <td class="col5">PRENOM</td>
		    <td class="col2">DATE DE NAISSANCE</td>
		  </tr>
     </table>

	<div class="content_table bright">
	<table class="tab_list" >
  	<?php 
	$afficherMotif = false;
	if(isset($row_rs1['Identifiant_personne']) && !empty($row_rs1['Identifiant_personne']) && $etatContrat == 'A' && strcmp($motifEvenement, 'NP')!=0  && strcmp($motifEvenement, 'S')!=0  && strcmp($motifEvenement, 'RE')!=0  && strcmp($motifEvenement, 'CC')!=0)
	{ 
		$i=1; 
  		do 
  		{ 
	  		$afficherMotif = true;
			mysql_select_db($database_microassur, $microassur);
			$query_rs_lab_personne = "SELECT * FROM personne WHERE Identifiant_personne = '". $row_rs1['Identifiant_personne'] ."'";
			$rs_lab_personne = mysql_query($query_rs_lab_personne, $microassur) or die(mysql_error());
			$row_rs_lab_personne = mysql_fetch_assoc($rs_lab_personne);
			$totalRows_rs_lab_personne = mysql_num_rows($rs_lab_personne);
			
			$sTypeRole = dataObject_elementdico::elementdico_getNomFromCode($row_rs1['Type_role'], dataObject_dictionnaire::_TYPE_ROLE);
			$sTypeAssure = dataObject_elementdico::elementdico_getNomFromCode($row_rs1['Type_assure'], dataObject_dictionnaire::TYPE_ASSURE);
  	?>
    <tr <?php if($i%2==0) echo 'class="colorligne"'; ?>>
    	<td class="col1">
      		<input onClick="modifier(this.value);" type="radio" name="choix" id="choix" value="<?php echo $row_rs1['Identifiant_personne']; ?>" />
      	</td>	
      	<td class="col2"><?php echo $sTypeRole ?></td>
	 	<?php //$tab_typeAssure = array('AS'=>'Ascendant', 'AU'=>'Autre', 'C'=>'Conjoint', 'E'=>'Enfant', 'S'=>'Souscripteur'); ?>
      	<td class="col3"><?php echo $sTypeAssure?>&nbsp;</td>
      	<td class="col4"><?php echo $row_rs_lab_personne['Nom_personne']; ?></td>
      	<td class="col5"><?php echo $row_rs_lab_personne['Prenom_personne']; ?></td>
      	<td class="col2">&nbsp;<?php echo dateEN2FR($row_rs_lab_personne['Date_naissance_personne']); ?></td>
    </tr>
    <?php 
    $i++;
		} while ($row_rs1 = mysql_fetch_assoc($rs1)); 
		mysql_free_result($rs1);
		mysql_free_result($rs_lab_personne);
	} // Fin if(isset($row_rs1['Identifiant_personne']) && !empty($row_rs1['Identifiant_personne']))
	?>
	</table>
  
	</div>
	<div id="buttonright"><span class="btleft"></span><span class="btmiddle"><a id="modifAct" href="#">Modifier</a></span><span class="btright"></span></div>
	<div id="buttonright"><span class="btleft"></span><span class="btmiddle"><a id="VisuaAct" href="#">Visualiser</a></span><span class="btright"></span></div>
</div>
	

<div class="imgright2"></div>
<div class="cleared"></div>

<form action="<?php echo $editFormAction; ?>" method="post" name="form2" id="form2">

  <div class="bt1">
	<p align="center"><a href="../../menu.php">
    	<span class="btleft"></span>
        <span class="btmiddle">Retour</span>
        <span class="btright"></span>
    </a></p>
  </div>
  
 
 
</form>
</div>
</body>
</html>
<?php include_once PATH_INCLUDE.'inc_pied_page.php'; ?>
